/*
 * Copyright (C) 2012 Virglinio & Asociados
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.virglinio.telemedicina.rehabilitacion.openGL;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Rectangulo {
	
	private FloatBuffer vertexBuffer;  // Buffer for vertex-array
	
	private int numFaces = 6;
	
	private float[][] colors = {  		// Colors of the 6 faces
			{1.0f, 0.5f, 0.0f, 1.0f},  	// 0. orange
			{1.0f, 0.0f, 1.0f, 1.0f},  	// 1. violet
			{0.0f, 1.0f, 0.0f, 1.0f},  	// 2. green
			{0.0f, 0.0f, 1.0f, 1.0f},  	// 3. blue
			{1.0f, 0.0f, 0.0f, 1.0f},  	// 4. red
			{1.0f, 1.0f, 0.0f, 1.0f}   	// 5. yellow
	};
	
	private float[] vertices = {  	// Vertices of the 6 faces
			// FRONT
			-1.0f, -1.0f,  2.0f,  	// 0. left-bottom-front
			1.0f, -1.0f,  2.0f,  	// 1. right-bottom-front
			-1.0f,  1.0f,  2.0f,  	// 2. left-top-front
			1.0f,  1.0f,  2.0f,  	// 3. right-top-front
			// BACK
			1.0f, -1.0f, -2.0f,  	// 6. right-bottom-back
			-1.0f, -1.0f, -2.0f,  	// 4. left-bottom-back
			1.0f,  1.0f, -2.0f,  	// 7. right-top-back
			-1.0f,  1.0f, -2.0f,  	// 5. left-top-back
			// LEFT
			-1.0f, -1.0f, -2.0f,  	// 4. left-bottom-back
			-1.0f, -1.0f,  2.0f,  	// 0. left-bottom-front 
			-1.0f,  1.0f, -2.0f,  	// 5. left-top-back
			-1.0f,  1.0f,  2.0f,  	// 2. left-top-front
			// RIGHT
			1.0f, -1.0f,  2.0f,  	// 1. right-bottom-front
			1.0f, -1.0f, -2.0f,  	// 6. right-bottom-back
			1.0f,  1.0f,  2.0f,  	// 3. right-top-front
			1.0f,  1.0f, -2.0f,  	// 7. right-top-back
			// TOP
			-1.0f,  1.0f,  2.0f,  	// 2. left-top-front
			1.0f,  1.0f,  2.0f,  	// 3. right-top-front
			-1.0f,  1.0f, -2.0f,  	// 5. left-top-back
			1.0f,  1.0f, -2.0f,  	// 7. right-top-back
			// BOTTOM
			-1.0f, -1.0f, -2.0f,  	// 4. left-bottom-back
			1.0f, -1.0f, -2.0f,  	// 6. right-bottom-back
			-1.0f, -1.0f,  2.0f,  	// 0. left-bottom-front
			1.0f, -1.0f,  2.0f   	// 1. right-bottom-front
	};
	
	public Rectangulo(){
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuffer.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
	}

	public void draw(GL10 gl) {
		gl.glFrontFace(GL10.GL_CCW);    // Front face in counter-clockwise orientation
		gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
		gl.glCullFace(GL10.GL_BACK);    // Cull the back face (don't display)

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

		// Render all the faces
		for (int face = 0; face < numFaces; face++) {
			// Set the color for each of the faces
			gl.glColor4f(colors[face][0], colors[face][1], colors[face][2], colors[face][3]);
			// Draw the primitive from the vertex-array directly
			gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face * 4, 4);
		}
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
	}
}
